package edu.ynu.se.xiecheng.achitectureclass.project.controller;

import edu.ynu.se.xiecheng.achitectureclass.project.common.Result;
import edu.ynu.se.xiecheng.achitectureclass.project.po.entity.Data;
import edu.ynu.se.xiecheng.achitectureclass.project.service.SoilDataService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/api/soil-data")
public class SoilDataController {

    @Autowired
    private SoilDataService soilDataService;

    /**
     * 获取并存储土壤数据
     *
     * @return 存储的土壤数据列表
     */
    @PostMapping("/fetch")
    public Result<List<Data>> fetchAndSaveSoilData() {
        try {
            List<Data> soilDataList = soilDataService.fetchAndSaveSoilData();
            return Result.success(soilDataList);
        } catch (Exception e) {
            return Result.error(500, e.getMessage());
        }
    }
}